//
//  ListenCategoryView.m
//  JianChaRiBao
//
//  Created by yuzhou on 13-5-22.
//  Copyright (c) 2013年 wzyk. All rights reserved.
//

#import "ListenCategoryView.h"
#import "ListenCell.h"
#import "AppDelegate.h"
#import "ListenInfoViewController.h"

@interface ListenCategoryView ()
@property (nonatomic,retain) NSArray *listenArray;
@end

@implementation ListenCategoryView

- (void)awakeFromNib
{
    [super awakeFromNib];
    listenGridView.requiresSelection = NO;
    listenGridView.layoutDirection = AQGridViewLayoutDirectionHorizontal;
    listenGridView.showsHorizontalScrollIndicator = NO;
}

- (void)setCategory:(NSDictionary *)category
{
    if (category && ![category isEqualToDictionary:_category]) {
        [_category release];
        _category = [category retain];
        [self resetDisplay];
        [self getData];
    }
}

- (void)getData
{
    NSString *classID = [_category objectForKey:@"class_id"];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSDictionary *data = [[DataRequest sharedRequest] getListenByCatgory:[_category objectForKey:@"class_id"]];
        dispatch_async(dispatch_get_main_queue(), ^{
            if (![classID isEqualToString:[_category objectForKey:@"class_id"]]) {
                return ;
            }
            if (data && [data isKindOfClass:[NSDictionary class]] && [[data objectForKey:@"result"] isKindOfClass:[NSArray class]]) {
                self.listenArray = [data objectForKey:@"result"];
                [listenGridView reloadData];
                [self scrollViewDidEndDecelerating:listenGridView];
            }
        });
    });
}

- (void)resetDisplay
{
    self.listenArray = nil;
    [listenGridView reloadData];
    leftArrow.hidden = YES;
    rightArrow.hidden = YES;
    nameLabel.text = [_category objectForKey:@"class_name"];
}

- (NSUInteger)numberOfItemsInGridView:(AQGridView *)gridView
{
    return _listenArray.count;
}

- (AQGridViewCell *)gridView:(AQGridView *)gridView cellForItemAtIndex:(NSUInteger)index
{
    static NSString *CellIdentify = @"ListenCell";
    ListenCell *cell = (ListenCell *)[gridView dequeueReusableCellWithIdentifier:CellIdentify];
    if (cell == nil) {
        cell = [[ListenCell alloc] initWithFrame:CGRectMake(0, 0, 70, 100) reuseIdentifier:CellIdentify];
    }
    cell.listenInfo = [_listenArray objectAtIndex:index];
    [cell updateDisplay];
    return cell;
}

- (CGSize)portraitGridCellSizeForGridView:(AQGridView *)gridView
{
    return CGSizeMake(94, 100);
}

- (void)gridView:(AQGridView *)gridView didSelectItemAtIndex:(NSUInteger)index
{
    ListenInfoViewController *listenInfoViewController = [[[ListenInfoViewController alloc] initWithNibName:@"ListenInfoViewController" bundle:nil] autorelease];
    listenInfoViewController.listenID = [[_listenArray objectAtIndex:index] objectForKey:@"item_id"];
    listenInfoViewController.listenName = [[_listenArray objectAtIndex:index] objectForKey:@"item_name"];
    [[UIApplication rootNavController] pushViewController:listenInfoViewController animated:YES];
    [gridView deselectItemAtIndex:index animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    leftArrow.hidden = (scrollView.contentOffset.x > 0) ? NO :YES;
    rightArrow.hidden = (scrollView.contentOffset.x < scrollView.contentSize.width - scrollView.bounds.size.width) ? NO :YES;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (decelerate == NO) {
        [self scrollViewDidEndDecelerating:scrollView];
    }
}

- (void)dealloc {
    [_category release];
    [_listenArray release];
    [nameLabel release];
    [listenGridView release];
    [leftArrow release];
    [rightArrow release];
    [super dealloc];
}
@end
